package com.block.datapush.common.filter.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.block.common.service.LogFactory;
import com.block.datapush.common.enums.FilterErrorEnum;
import com.block.datapush.common.filter.IFilterHandlerService;
import com.block.datapush.common.model.param.FilterHandlerParam;
import com.block.datapush.common.model.result.FilterHandlerResult;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * 判断每周不推的日期
 */
@Service
public class FilterWeekHandlerService implements IFilterHandlerService {

    private static Logger log = LogFactory.getPushLog();

    @Override
    public FilterHandlerResult handlerFilterLogic(FilterHandlerParam handlerParam, MemberExtInfo memberExtInfo) {

        String notPushDayWeek = handlerParam.getNotPushDayWeek();
        if (StrUtil.isEmpty(notPushDayWeek)){
            return FilterHandlerResult.success();
        }

        Integer nowDate = LocalDate.now().getDayOfWeek().getValue();
        List<Integer> dayWeekList = JSON.parseArray(notPushDayWeek, Integer.class);
        if (CollUtil.isEmpty(dayWeekList) || !dayWeekList.contains(nowDate)){
            return FilterHandlerResult.success();
        }

        String message = StrUtil.format("【filter】{}【value】{}", notPushDayWeek, nowDate);
        log.info("【机构条件判断】false【不推送日/周】{}", message);
        return FilterHandlerResult.error(FilterErrorEnum.DAYWEEK_ERROR, message);
    }

}
